﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using ICD10WebApiOData.DTO;
using ICD10WebApiOData.Models;

namespace ICD10WebApiOData.Controllers
{
    /*
    To add a route for this controller, merge these statements into the Register method of the WebApiConfig class. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using ICD10WebApiOData.Models;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<ICD10Chapter>("ICD10Chapter");
    config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
    */
    public class ICD10ChapterController : ODataController
    {
        private ICD10BEntities db = new ICD10BEntities();

        // GET odata/ICD10Chapter
        [Queryable]
        public IQueryable<ICDChapter> GetICD10Chapter()
        {
            var result = from icd in db.ICD10Chapter                         
                         select new ICDChapter()
                         {
                             ID = icd.ID,
                             ChapID = icd.ChapID,
                             ChapterName = icd.ChapterName,
                             Name = icd.Name,
                             Include = icd.Include,
                             Incl = icd.Incl,
                             Additional = icd.Additional,
                             Excl = icd.Excl,
                             Note = icd.Note                             
                         };
            return result;
        }

        // GET odata/ICD10Chapter(5)
        [Queryable]
        public SingleResult<ICDChapter> GetICD10Chapter([FromODataUri] int key)
        {
            var result = from icd in db.ICD10Chapter
                where icd.ID == key
                select new ICDChapter()
                       {
                           ID = icd.ID,
                           ChapID = icd.ChapID,
                           ChapterName = icd.ChapterName,
                           Name = icd.Name,
                           Include = icd.Include,
                           Incl = icd.Incl,
                           Additional = icd.Additional,
                           Excl = icd.Excl,
                           Note = icd.Note         
                       };

            return SingleResult.Create(result);
        }

        ////[HttpPost]
        ////public IQueryable<ICDChapter> GetChapterContainByChapID(ODataActionParameters parameters)
        ////{
        ////    int chaoID = (int)parameters["ChapID"];

        ////    return db.ChapterContains.Where(x => x.ChapterID == chaoID);
        ////}

        // PUT odata/ICD10Chapter(5)
        public IHttpActionResult Put([FromODataUri] int key, ICD10Chapter icd10chapter)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != icd10chapter.ID)
            {
                return BadRequest();
            }

            db.Entry(icd10chapter).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ICD10ChapterExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(icd10chapter);
        }

        // POST odata/ICD10Chapter
        public IHttpActionResult Post(ICD10Chapter icd10chapter)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.ICD10Chapter.Add(icd10chapter);
            db.SaveChanges();

            return Created(icd10chapter);
        }

        // PATCH odata/ICD10Chapter(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public IHttpActionResult Patch([FromODataUri] int key, Delta<ICD10Chapter> patch)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ICD10Chapter icd10chapter = db.ICD10Chapter.Find(key);
            if (icd10chapter == null)
            {
                return NotFound();
            }

            patch.Patch(icd10chapter);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ICD10ChapterExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(icd10chapter);
        }

        // DELETE odata/ICD10Chapter(5)
        public IHttpActionResult Delete([FromODataUri] int key)
        {
            ICD10Chapter icd10chapter = db.ICD10Chapter.Find(key);
            if (icd10chapter == null)
            {
                return NotFound();
            }

            db.ICD10Chapter.Remove(icd10chapter);
            db.SaveChanges();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ICD10ChapterExists(int key)
        {
            return db.ICD10Chapter.Count(e => e.ID == key) > 0;
        }
    }
}
